//
//  UIImageView+CustomCache.m
//  MiniShop
//
//  Created by wwhk on 9/13/14.
//  Copyright (c) 2014 mini. All rights reserved.
//

#import "UIImageView+CustomCache.h"
#import "UIImageView+WebCache.h"

@implementation UIImageView (CustomCache)

- (void)setImageWithURL:(NSURL *)url cachePath:(NSString *)path placeholderImage:(UIImage *)placeholder
{
    if (placeholder) {
        self.image = placeholder;
    }
    if (url) {
        SDImageCache * imageCache = [[SDImageCache alloc] initWithNamespace:path];
        [[SDImageCache sharedImageCache] addReadOnlyCachePath:path];
        NSString * key = [url absoluteString];
        
        UIImage * image = [imageCache imageFromDiskCacheForKey:key];
        if (image) {
            self.image = image;
        }else{
            [self sd_setImageWithURL:url placeholderImage:placeholder options:SDWebImageCacheMemoryOnly|SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                [imageCache storeImage:image forKey:key];
            }];
        }
    }
}

@end
